package com.yuncheng.spcyApi.flow;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.constant.flow.FlowNodeConstant;
import com.yuncheng.spcyApi.entity.SysJob;
import com.yuncheng.spcyApi.entity.SysRole;
import com.yuncheng.entity.YcSysOrg;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.YcSysUserMapper;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.utils.StringPatternUtils;
import com.yuncheng.spcyApi.vo.flow.AgentVo;
import com.yuncheng.vo.GetKsKz;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component(value = "FlowNodeDbrService")
public class FlowNodeDbrService {

    @Resource
    @Lazy
    private ISysRoleService sysRoleService;

    @Resource
    @Lazy
    private YcSysUserMapper ycSysUserMapper;

    @Resource
    @Lazy
    private IYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private ISysJobService sysJobService;

    @Resource
    @Lazy
    private IYcSysOrgService ycSysOrgService;

    @Resource
    @Lazy
    private ISysUserFgldService sysUserFgldService;

    //获取中心职员
    public Map getzxzy(){
        List<SysRole> list = sysRoleService.list(new LambdaQueryWrapper<SysRole>().eq(SysRole::getRoleName, FlowNodeConstant.ROLE_NAME_ZXZY));
        SysRole sysRoles = list.get(0);
        String zxzyid= sysRoles.getId();
        List<YcSysUser> zxzyList = ycSysUserMapper.getZxzy(zxzyid);
        Map map = new HashMap();
        String name = "";
        String dbrid = "";
        for (YcSysUser zxzy:zxzyList){
            if (StringUtils.isBlank(dbrid)){
                name += zxzy.getId();
                dbrid += zxzy.getRealname();
            }else {
                name += "," + zxzy.getId();
                dbrid += "," + zxzy.getRealname();
            }

        }
        map.put("dbrId", dbrid);
        map.put("dbr", name);
        return map;
    }


    //根据岗位id 查询待办人
    public Map getDbrByGwid(String gwIds){
        if (StringUtils.isBlank(gwIds)){
            throw  new BusinessException("岗位不存在");
        }
        Map map = new HashMap();
        String name = "";
        String dbrid = "";
        //查询当前岗位id下的所有人
        List<YcSysUser> gwDbr = ycSysUserMapper.getGwDbr(gwIds);
        if (CollectionUtil.isEmpty(gwDbr)){
            throw new BusinessException("岗位不存在");
        }
        for (YcSysUser gw:gwDbr){
            if (StringUtils.isBlank(dbrid)){
                name += gw.getRealname();
                dbrid += gw.getId();
            }else {
                name += ","+gw.getRealname() + ",";
                dbrid += ","+gw.getId() + ",";
            }

        }
        map.put("dbrId", dbrid);
        map.put("dbr", name);
        return map;
    }

    //根据部门id 查询待办人 和待办人id
    public Map getDbrByBmid(String departIds){
        String dbr="";
        String dbrid="";
        Map map = new HashMap();
        List<YcSysUser> userByDepartId = ycSysUserMapper.getUserByDepartId(departIds);
        if (userByDepartId == null){
            throw  new BusinessException("用户不存在");
        }
        for (YcSysUser user: userByDepartId){
            if (StringUtils.isBlank(dbr) && StringUtils.isBlank(dbrid)){
                dbr += user.getRealname();
                dbrid += user.getId();
            }else {
                dbr += ","+ user.getRealname();
                dbrid += ","+ user.getId();
            }
            map.put("dbrId",dbrid);
            map.put("dbr",dbr);
        }
        return map;
    }

    //根据部门id 和 岗位id 查询待办人
    public Map getdbrByDeptName(String deptNameid,String gwid){
        YcSysOrg ycSysOrg = ycSysOrgService.getById(deptNameid);
        SysJob sysJob = sysJobService.getById(gwid);
        String orgName = ycSysOrg.getOrgName();
        String jobName = sysJob.getJobName();
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";
        List<GetKsKz> ksKzObject = new ArrayList<>();
        ksKzObject = ycSysUserService.getKsKzObject(orgName,jobName);
        if (CollectionUtils.isNotEmpty(ksKzObject)) {
            for (GetKsKz ksKz : ksKzObject) {
                if (StringUtils.isBlank(dbrId)){
                    dbrId += ksKz.getId();
                    dbr += ksKz.getRealname();
                }else {
                    dbrId += "," + ksKz.getId();
                    dbr += "," + ksKz.getRealname();
                }

            }
        }

        map.put("dbrId", dbrId);
        map.put("dbr", dbr);
        return map;
    }

    // 根据待办人id 拼接待办人姓名
    public String getDbrNameById(String ids){
        if (StringUtils.isBlank(ids)){
            throw new BusinessException("获取不到环节待办人！");
        }

        List<String> idsList = StringPatternUtils.getListByIds(ids);
        String name = "";
        List<YcSysUser> userList = ycSysUserService.findRealnameListByIds(idsList);

        if (CollectionUtil.isEmpty(userList)){
            throw new BusinessException("用户不存在！");
        }

        for (String id : idsList){
            List<YcSysUser> filterList = userList.stream()
                    .filter(f -> f.getId().equals(id))
                    .collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(filterList)){
                name += StringUtils.isNotBlank(name) ? "," : "";
                name += filterList.get(0).getRealname();
            }
        }

        /*for (YcSysUser user : userList){
            if (StringPatternUtils.verifryIsEmptyStringBuffer(name)){
                name.append(user.getRealname());
            }else {
                name.append(",").append(user.getRealname());
            }
        }*/
        return name;
    }

    /**
     * 将待办人map转为List
     * @param map 待办人map
     */
    public List<AgentVo> getDbrListByMap(Map map){
        String dbrids = map.get("dbrId").toString(); // 待办人id
        String dbrs = map.get("dbr").toString(); // 待办人

        List<AgentVo> agentList = new ArrayList<>();

        if (org.apache.commons.lang3.StringUtils.isNotBlank(dbrids)) {
            String agentIds[] = dbrids.split(",");
            String agentNames[] = dbrs.split(",");

            for (int i = 0; i < agentIds.length; i++) {
                AgentVo agentVo = new AgentVo();
                agentVo.setDbrid(agentIds[i]);
                agentVo.setDbr(agentNames[i]);
                agentList.add(agentVo);
            }
        }
        return agentList;
    }


    /**
     * 获取分管领导-根据部门id
     */
    public Map getFgldByDeptId(String bmid){
        Map map = new HashMap();
        String dbrId = "";
        String dbr = "";

        YcSysUser fgld = sysUserFgldService.getFgldUserBydeptId(bmid);
        if (fgld != null) {
            dbrId = fgld.getId();
            dbr = fgld.getRealname();
        }

        map.put("dbrId",dbrId);
        map.put("dbr",dbr);

        return map;
    }
}
